﻿using AutoMapper;
using DFGK_WMS.Context;
using DFGK_WMS.IRepository;
using DFGK_WMS.Model.DTO;
using DFGK_WMS.Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DFGK_WMS.Repository
{
    public class TMaterialReportRepository : BaseRepository<TMaterial>, ITMaterialReportRepository
    {
        public TMaterialReportRepository(DFGK_DB context, IMapper mapper) : base(context, mapper)
        {
        }

        /// <summary>
        /// 进销存报表（出入库）
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public List<StereoscopicLibraryDto> GetInboundAndOutbound(out int totalCount, out string errorMsg,string ? date, int pageIndex, int pageSize)
        {
            totalCount = 0;
            errorMsg = null;
            List<StereoscopicLibraryDto> inboundAndOutbound = new List<StereoscopicLibraryDto>();

            var list = Context.TMaterial.AsQueryable();
            List<TMaterial> TMaterialsList = null;
            if (date == null)
            {
                TMaterialsList = list.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            }
            else
            {
                TMaterialsList = list.Skip((pageIndex - 1) * pageSize).Take(pageSize).Where(x => date.Equals(x.CreateDate.ToString().Substring(0, 10))).ToList();

            }

            if (TMaterialsList == null)
            {
                errorMsg = "没有查询到数据";
                return null;
            }
            totalCount = TMaterialsList.Count;
            foreach (var material in TMaterialsList)
            {
                var dto = new StereoscopicLibraryDto();

                dto.NameAndSpecifications = material.Name + material.ModelSpec;

                var TInventoryList = Context.TInventories.Where(x => x.MaterialCode == material.Code);
                if (TInventoryList == null)
                {
                    continue;
                }

                foreach (var inventory in TInventoryList)
                {
                    dto.InitialQty = inventory.AvailableQty;
                    var TotalPriceG = inventory.TotalPrice * 1000000;
                    var TotalPriceT = TotalPriceG / (inventory.AllocateQty + inventory.AvailableQty) * inventory.AvailableQty;
                    dto.InitialTonnage = TotalPriceT / 1000000;

                    switch (inventory.BillType)
                    {
                        // 本地入库
                        case 1:
                            dto.LocalReceivingNum = inventory.PutawayQty;
                            dto.LocalReceivingWeight = inventory.PutawayQty * TotalPriceG / 1000000;
                            break;
                        // 生产订单入库
                        case 2:
                            dto.ProductionOrderNum = inventory.PutawayQty;
                            dto.ProductionOrderWeight = inventory.PutawayQty * TotalPriceG / 1000000;
                            break;
                        // 采购订单入库
                        case 3:
                            dto.PurchaseOrderNum = inventory.PutawayQty;
                            dto.PurchaseOrderWeight = inventory.PutawayQty * TotalPriceG / 1000000;
                            break;
                        // 成本中心退料
                        case 4:
                            dto.CostCenterReturnNum = inventory.PutawayQty;
                            dto.CostCenterReturnWeight = inventory.PutawayQty * TotalPriceG / 1000000;
                            break;
                        // 空托盘
                        case 5:
                            dto.EmptyTrayNum = inventory.PutawayQty;
                            dto.EmptyTrayWeight = inventory.PutawayQty * TotalPriceG / 1000000;
                            break;
                        // 出库单
                        case 1120:
                            dto.OutboundOrderNum = inventory.OutQty;
                            dto.OutboundOrderWeight = inventory.OutQty * TotalPriceG / 1000000;
                            break;
                        // 调拨出库
                        case 1141:
                            dto.TransferOutboundNum = inventory.OutQty;
                            dto.TransferOutboundWeight = inventory.OutQty * TotalPriceG / 1000000;
                            break;
                        // 内配出库
                        case 1142:
                            dto.InternalOutboundNum = inventory.OutQty;
                            dto.InternalOutboundWeight = inventory.OutQty * TotalPriceG / 1000000;
                            break;
                    }
                }

                inboundAndOutbound.Add(dto);
            }

            return inboundAndOutbound;
        }

        /// <summary>
        /// 物料报表
        /// </summary>
        /// <param name="totalCount"></param>
        /// <param name="MaterialsName"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public List<TMaterialReporiDto> GetMaterials(out int totalCount, string? MaterialsName,string ?MaterialsCode, int pageIndex, int pageSize)
        {
            totalCount = 0;

            var list = Context.TInventories.AsQueryable();

            if (MaterialsName != null)
            {
                list = list.Where(x => x.MaterialName == MaterialsName);
            }


            totalCount = list.Count();

            var items = list.OrderBy(x => x.MaterialName)
                .Skip((pageIndex - 1) * pageSize)
                .Take(pageSize)
                .ToList()
                .Select(x =>
                {
                    TMaterial item = null;
                    if (MaterialsName != null)
                    {
                        item = Context.TMaterial.FirstOrDefault(x => x.Code == MaterialsCode);
                    }

                    return new TMaterialReporiDto
                    {
                        materialId = x.MaterialId,
                        materialName = x.MaterialName,
                        materialCode = x.MaterialCode,
                        ModelSpec = item == null ? null : item.ModelSpec,
                        BatchNo = x.BatchNo,
                        InStockDate = x.InStockDate,
                        MaterialTypeName = x.MaterialTypeName,
                        Status = x.Status,
                        AvailableQty = x.AvailableQty,
                        UnitName = x.UnitName
                    };

                }).ToList();

            return items;
        }
    }
}
